package com.xszx.server.service;

import com.xszx.server.dto.Page;
import com.xszx.server.entity.${Domain};
import com.xszx.server.mapper.${Domain}Mapper;
import com.xszx.server.util.UuidUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
<#list typeSet as type>
    <#if type=='Date'>
import java.util.Date;
    </#if>
</#list>

@Service
public class ${Domain}Service {
    @Resource
    private ${Domain}Mapper ${domain}Mapper;

    /**
     * 获取分页列表
     * @param page
     * @return
     */
    public Page getAll${Domain}s(Page page){
        int pageNum = page.getPage();
        page.setPage((page.getPage()-1)*page.getSize());
        page.setList(${domain}Mapper.findAll(page));
        page.setTotal(${domain}Mapper.findCount());
        page.setPage(pageNum);
        return page;
    }

    /**
     * 新增或者删除信息
     * @param ${domain}
     * @return
     */
    public String saveOrUpdate(${Domain} ${domain}){
        if(StringUtils.isEmpty(${domain}.getId())){
            <#list typeSet as type>
                <#if type=='Date'>
            Date now = new Date();
                </#if>
            </#list>
            <#list fieldList as field>
                <#if field.nameHump=='createdAt'>
            ${domain}.setCreatedAt(now);
                </#if>
                <#if field.nameHump=='updatedAt'>
            ${domain}.setUpdatedAt(now);
                </#if>
            </#list>
            ${domain}.setId(UuidUtil.getShortUuid());
            return (${domain}Mapper.insert(${domain})>0)?"添加成功":"添加失败";
        }else{
            <#list fieldList as field>
                <#if field.nameHump=='updatedAt'>
            ${domain}.setUpdatedAt(new Date());
                </#if>
            </#list>
            return (${domain}Mapper.updateByPrimaryKey(${domain})>0)?"更新成功":"更新失败";
        }
    }

    /**
     * 物理删除${tableNameCn}
     * @param id
     * @return
     */
    public int del(String id){
        return ${domain}Mapper.deleteByPrimaryKey(id);
    }
     /**
     * 获取单个${tableNameCn}
     * @return
     */
    public ${Domain} get${Domain}(String id){
        return ${domain}Mapper.selectByPrimaryKey(id);
    }
}
